home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / xvisrc.zip / ASCII.C < prev    next >
C/C++ Source or Header  |  1992-07-28  |  3KB  |  114 lines

  1. /* Copyright (c) 1990,1991,1992 Chris and John Downey */
  2. #ifndef lint
  3. static char *sccsid = "@(#)ascii.c    2.1 (Chris & John Downey) 7/29/92";
  4. #endif
  5.  
  6. /***
  7.  
  8. * program name:
  9.     xvi
  10. * function:
  11.     PD version of UNIX "vi" editor, with extensions.
  12. * module name:
  13.     ascii.c
  14. * module function:
  15.     Visual representations of control & other characters.
  16.  
  17.     This file is specific to the ASCII character set;
  18.     versions for other character sets could be implemented if
  19.     required.
  20. * history:
  21.     STEVIE - ST Editor for VI Enthusiasts, Version 3.10
  22.     Originally by Tim Thompson (twitch!tjt)
  23.     Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
  24.     Heavily modified by Chris & John Downey
  25.  
  26. ***/
  27.  
  28. #include "xvi.h"
  29.  
  30. /*
  31.  * Output visual representation for a character. Return value is the
  32.  * width, in display columns, of the representation; the actual string
  33.  * is returned in *pp unless pp is NULL.
  34.  *
  35.  * If tabcol is -1, a tab is represented as "^I"; otherwise, it gives
  36.  * the current physical column, & is used to determine the number of
  37.  * spaces in the string returned as *pp.
  38.  *
  39.  * Looks at the parameters tabs, list, cchars and mchars.
  40.  * This may sound costly, but in fact it's a single memory
  41.  * access for each parameter.
  42.  */
  43. unsigned
  44. vischar(c, pp, tabcol)
  45.     register int    c;
  46.     register char    **pp;
  47.     register int    tabcol;
  48. {
  49.     static char        crep[5];
  50.  
  51.     if (c == '\t' && tabcol >= 0 && Pb(P_tabs) && !Pb(P_list)) {
  52.     /*
  53.      * Tab which we have to display as a string of
  54.      * spaces (rather than "^I" or directly).
  55.      */
  56.     register unsigned    nspaces;
  57.  
  58.     while (tabcol >= Pn(P_tabstop))
  59.         tabcol -= Pn(P_tabstop);
  60.     nspaces = Pn(P_tabstop) - tabcol;
  61.     if (pp != NULL) {
  62.         static char    spstr[MAX_TABSTOP + 1];
  63.         static unsigned    lastnum;
  64.  
  65.         /*
  66.          * Paranoia (maybe).
  67.          */
  68.         if (nspaces > MAX_TABSTOP)
  69.         nspaces = MAX_TABSTOP;
  70.         if (nspaces > lastnum)
  71.         (void) memset(&spstr[lastnum], ' ',
  72.                (int) (nspaces - lastnum));
  73.         spstr[lastnum = nspaces] = '\0';
  74.         *pp = spstr;
  75.     }
  76.     return nspaces;
  77.     } else if (((unsigned char) c < ' ' || c == DEL) && !Pb(P_cchars)) {
  78.     /*
  79.      * ASCII Control characters.
  80.      */
  81.     if (pp != NULL) {
  82.         *pp = crep;
  83.         crep[0] = '^';
  84.         crep[1] = (c == DEL ? '?' : c + ('A' - 1));
  85.         crep[2] = '\0';
  86.     }
  87.     return 2;
  88.     } else if ((c & ~0177) && !Pb(P_mchars)) {
  89.     /*
  90.      * If Pb(P_mchars) is unset, we display non-ASCII characters
  91.      * (i.e. top-bit-set characters) as octal escape sequences.
  92.      */
  93.     if (pp != NULL) {
  94.         *pp = crep;
  95.         crep[0] = '\\';
  96.         crep[1] = ((c >> 6) & 7) + '0';
  97.         crep[2] = ((c >> 3) & 7) + '0';
  98.         crep[3] = (c        & 7) + '0';
  99.         crep[4] = '\0';
  100.     }
  101.     return 4;
  102.     } else {
  103.     /*
  104.      * Printable character.
  105.      */
  106.     if (pp != NULL) {
  107.         *pp = crep;
  108.         crep[0] = c;
  109.         crep[1] = '\0';
  110.     }
  111.     return 1;
  112.     }
  113. }
  114.